package com.linln.admin.service;


import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linln.common.vo.JsonResult;
import com.linln.modules.integralmall.domain.ServiceType;
import com.linln.modules.integralmall.service.ServiceTypeService;
import com.linln.modules.service.domain.Service;
import com.linln.modules.service.service.ServiceService;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Arrays;
import java.util.List;

/**
 * @author 钟世超
 * @since 2021-01-06
 * @version 1.0.7
 */
@RequestMapping("/service/setmeal")
@Controller
public class ServiceController {

	@Autowired
	private ServiceService serviceService;

	@Autowired
	private ServiceTypeService serviceTypeService;
	
	@RequiresPermissions("service:setmeal:index")
	@RequestMapping("/index")
	public String list() {
		return "/service/setmeal/list";
	}

	/**
	 * 获取分页数据
	 * @param page
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping("/list")
	@ResponseBody
	public JsonResult list(Page<Service> page //
		) throws JSONException { // 分页
		LambdaQueryWrapper<Service> queryWrapper = Wrappers.lambdaQuery();
		IPage<Service> ipage = serviceService.page(page, queryWrapper);
		for(Service service : ipage.getRecords()){
			if(Strings.isNotBlank(service.getServiceProperties())){
				JSONObject jsonObject = new JSONObject(service.getServiceProperties());
				String serviceTypeName = (String)jsonObject.get("serviceTypeName");
				service.setServiceType(serviceTypeName);
			}
		}
		return JsonResult.ok().put("data",ipage);
	}

	@RequestMapping("/add")
	
	public String add(Model model) {
		List<ServiceType> typeList = serviceTypeService.getIntegralMallServiceTypeList();
		model.addAttribute("typeList",typeList);
		return "/service/setmeal/add";
	}

	@RequestMapping("/edit/{serviceId}")
	public String edit(@PathVariable("serviceId") String serviceId, Model model) {
		Service service = serviceService.getById(serviceId);
		List<ServiceType> typeList = serviceTypeService.getIntegralMallServiceTypeList();
		model.addAttribute("typeList",typeList);
		model.addAttribute("service", service);
		return "/service/setmeal/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("service:add")
	public JsonResult save(Service service) throws JSONException {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("serviceTypeName",service.getServiceType());
		service.setServiceProperties(jsonObject.toString());
		boolean result = serviceService.save(service);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("service:edit")
	public JsonResult update(Service service) {
		boolean result = serviceService.updateById(service);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("service:remove")
	@RequestMapping("/remove")
	@ResponseBody
	public JsonResult remove(@NotNull(message = "serviceId 不能为空") String serviceId) {
		boolean result = serviceService.removeById(serviceId);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 批量删除
	 */
	@RequiresPermissions("service:batchRemove")
	@RequestMapping("/batchRemove")
	@ResponseBody
	public JsonResult batchRemove(
			@NotNull(message = "不能为空") @Size(min = 1, message = "请至少选择一个") @RequestParam("serviceIds[]") String[] serviceIds) {
		boolean result = serviceService.removeByIds(Arrays.asList(serviceIds));
		return result ? JsonResult.ok() : JsonResult.error();
	}

}